package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.ResPath;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.ResourcesPath;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.MouseEvent;
	
	import newx.com.control.comboBox.NxComboBoxEvent;
	import newx.com.control.radio.NxRadioGroup;
	import newx.com.control.window.NxWindowEvent;

	public class WlybBoxFightLogic
	{
		private var _ui:WlybBoxFightUI;
		public static var _self:WlybBoxFightLogic;
		/**数据模型*/
		private 	var _model			:ModelLocator;
		/**伙伴列表***/
		private var parArr:Array;
		/**宝箱ID**/
		private var boxId:int;
		
		private var radioGroup				:NxRadioGroup;
		
		private var parObjArr:Array;
		public function WlybBoxFightLogic()
		{
		}
		
		public static function getInstance():WlybBoxFightLogic
		{
			if(_self == null)
			{
				_self =new WlybBoxFightLogic();
			}
			return _self;
		}
		
		
		public function uiinit():void
		{
			_model = ModelLocator.getInstance();
			radioGroup = new NxRadioGroup([ui.radioPlayer,ui.radiopar]);
		}
		
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			ui.hide();
		}
		
		/**
		 *设置伙伴列表 
		 * 
		 */
		private function setPar():void
		{
			var view:View = _model.scene.GetView(View.VIEWPORT_PARTNER);
			var newEquip:ViewObject;
			
			parArr = [];
			parObjArr = [];
			view.getDataForEach(function (key:String,data:*):void
			{
				if(data is ViewObject)
				{
					var str:String	=	data.GetProperties("Name")+" Lv."
					+ data.GetProperties("Level")
					parArr.push({label:str,data:data});
					parObjArr .push(data);
				}
			});
			ui.combobox.dataProvider = parArr;
//			ResourcesPath.getHeadPic( _model.scene.Role.ModelId, _model.scene.Role.clotheModel );
			ui.playerpic.source = ResourcesPath.getHeadPic( _model.scene.Role.ModelId, _model.scene.Role.clotheModel );//ResPath.PHOTO + "head_" + _model.scene.Role.GetProperties("Model") + ".png";
			
			if(ui.combobox.getSelecedItem() != null)
			{
				var viewO:ViewObject = ui.combobox.getSelecedItem().data;
				ui.parpic.source = ResPath.PHOTO + "head_" + viewO.GetProperties("Model") + ".png";
				ui.lab4.htmlText = ui.combobox.title;
			}
			
		}
		
		private function onfightClick(e:MouseEvent):void
		{
			if(ui.combobox.getSelecedItem() == null)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHALLENGE_WLYB_BOX,boxId,parObjArr.indexOf(viewO)+1,5,4);
				return;
			}
			var viewO:ViewObject = ui.combobox.getSelecedItem().data;
			var playerIndex:int;
			var parIndex:int;
			
			//设置伙伴与主角的站位点
			if(radioGroup.selectedIndex == 0)
			{//主角在前
				playerIndex = 5;
				parIndex = 4;
			}else
			{//伙伴在前
				playerIndex = 4;
				parIndex = 5;
			}
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHALLENGE_WLYB_BOX,boxId,viewO.GetProperties("ConfigID"),playerIndex,parIndex);
		}
		
		
		public function onshowUI(lid:int):void
		{
			boxId = lid;
			ui.showOrHide();
		}
		
		
		/**
		 * box下拉菜单点击事件 
		 * @param e
		 * 
		 */		
		private function onBoxClickHandle(e:NxComboBoxEvent):void
		{
			var data:ObjectData	=	e.item.data as ObjectData;
			var headId:int			=	int(data.GetProperties("Model"));
			//bindpatenrExp(view,_partnerID);
			ui.parpic.source	=	ResourcesPath.getHeadPic(headId);
			ui.lab4.htmlText = ui.combobox.title;
		}
		
		public function BeforeShow():void
		{
			
			radioGroup.selectdNxRadio = ui.radioPlayer;
			setPar();
			addListenter();
		}
		
		public function AfterHide():void
		{
			removeListenter();
		}
		
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
			ui.windowBG	.addEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			ui.btn_fight.addEventListener(MouseEvent.CLICK,onfightClick);
			ui.combobox.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK,onBoxClickHandle);
		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
			ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			ui.btn_fight.removeEventListener(MouseEvent.CLICK,onfightClick);
			ui.combobox.removeEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK,onBoxClickHandle);
		}
		
		public function get ui():WlybBoxFightUI
		{
			if(_ui==null)
			{
				_ui	=	new WlybBoxFightUI();
			}
			return _ui
		}
	}
}